package com.ubao.location;

import com.ubao.R;
import com.ubao.util.log.Logger;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class LocationActivity extends Activity {
	LocationManager locationManager;
	String locationProvider;
	LocationListener locationListener;
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
//			Criteria criteria = new Criteria(); 
//		//  获得最好的定位效果 
//		criteria.setAccuracy(Criteria.ACCURACY_FINE); 
//		criteria.setAltitudeRequired(false); 
//		criteria.setBearingRequired(false); 
//		criteria.setCostAllowed(false); 
//		//  使用省电模式 
//		criteria.setPowerRequirement(Criteria.POWER_HIGH); 
//		//  获得当前的位置提供者 
//		String provider = locationManager.getBestProvider(criteria, true);
//		Logger.d("provider:" +provider);
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, locationListener);
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 0, locationListener);
		}};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_location);
		String locationProvider = LocationManager.NETWORK_PROVIDER;
		Log.d("Test","provider:" + locationProvider);
		// Or, use GPS location data:
		// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;


		
		// Acquire a reference  to the system Location Manager
		locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		Logger.d("gps:" + locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
		Logger.d("network:" + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
		Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
		Logger.d( "lastlocation:" + lastKnownLocation);
		// Define a listener that responds to location updates
		 locationListener = new LocationListener() {
		    public void onLocationChanged(Location location) {
		      // Called when a new location is found by the network location provider.
		      makeUseOfNewLocation(location);
		      Logger.d("changed");
		    }


			public void onStatusChanged(String provider, int status, Bundle extras) {
				Logger.d("onStatusChanged");
			}

		    public void onProviderEnabled(String provider) {
		    	Logger.d("onProviderEnabled");
		    }

		    public void onProviderDisabled(String provider) {
		    	Logger.d("onProviderDisabled");
		    }
		  };
		  handler.sendEmptyMessageDelayed(0, 50);
		// Register the listener with the Location Manager to receive location updates
		
		
	}
	
	
	
	private void makeUseOfNewLocation(Location location) {
		// TODO Auto-generated method stub
		Logger.d("location:" + location.getLongitude()+ "," + location.getLatitude());
	}
	
	private static final int TWO_MINUTES = 1000 * 60 * 2;

	/** Determines whether one Location reading is better than the current Location fix
	  * @param location  The new Location that you want to evaluate
	  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
	  */
	protected boolean isBetterLocation(Location location, Location currentBestLocation) {
	    if (currentBestLocation == null) {
	        // A new location is always better than no location
	        return true;
	    }

	    // Check whether the new location fix is newer or older
	    long timeDelta = location.getTime() - currentBestLocation.getTime();
	    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
	    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
	    boolean isNewer = timeDelta > 0;

	    // If it's been more than two minutes since the current location, use the new location
	    // because the user has likely moved
	    if (isSignificantlyNewer) {
	        return true;
	    // If the new location is more than two minutes older, it must be worse
	    } else if (isSignificantlyOlder) {
	        return false;
	    }

	    // Check whether the new location fix is more or less accurate
	    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
	    boolean isLessAccurate = accuracyDelta > 0;
	    boolean isMoreAccurate = accuracyDelta < 0;
	    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

	    // Check if the old and new location are from the same provider
	    boolean isFromSameProvider = isSameProvider(location.getProvider(),
	            currentBestLocation.getProvider());

	    // Determine location quality using a combination of timeliness and accuracy
	    if (isMoreAccurate) {
	        return true;
	    } else if (isNewer && !isLessAccurate) {
	        return true;
	    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
	        return true;
	    }
	    return false;
	}

	/** Checks whether two providers are the same */
	private boolean isSameProvider(String provider1, String provider2) {
	    if (provider1 == null) {
	      return provider2 == null;
	    }
	    return provider1.equals(provider2);
	}
	
	

}
